﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Netcached.Tests
{
    [TestClass]
    public class TestGetSet : BaseTestClass
    {
        [TestMethod]
        public void TestSetInt16()
        {
            string key = "TestGetSet.TestSetInt16";
            short value = -2342;

            Client.Set(key, value);
            short fetchedValue = Client.Get<short>(key);

            Assert.AreEqual(value, fetchedValue);
        }

        [TestMethod]
        public void TestSetUInt16()
        {
            string key = "TestGetSet.TestSetUInt16";
            ushort value = 3456;

            Client.Set(key, value);
            ushort fetchedValue = Client.Get<ushort>(key);

            Assert.AreEqual(value, fetchedValue);
        }

        [TestMethod]
        public void TestSetInt32()
        {
            string key = "TestGetSet.TestSetInt32";
            int value = -234343;

            Client.Set(key, value);
            int fetchedValue = Client.Get<int>(key);

            Assert.AreEqual(value, fetchedValue);
        }

        [TestMethod]
        public void TestSetUInt32()
        {
            string key = "TestGetSet.TestSetUInt32";
            uint value = 345345;

            Client.Set(key, value);
            uint fetchedValue = Client.Get<uint>(key);

            Assert.AreEqual(value, fetchedValue);
        }

        [TestMethod]
        public void TestSetInt64()
        {
            string key = "TestGetSet.TestSetInt64";
            long value = -56454563653;

            Client.Set(key, value);
            long fetchedValue = Client.Get<long>(key);

            Assert.AreEqual(value, fetchedValue);
        }

        [TestMethod]
        public void TestSetUInt64()
        {
            string key = "TestGetSet.TestSetUInt64";
            ulong value = 768754232;

            Client.Set(key, value);
            ulong fetchedValue = Client.Get<ulong>(key);

            Assert.AreEqual(value, fetchedValue);
        }

        [TestMethod]
        public void TestSetByteArray()
        {
            string key = "TestGetSet.TestSetByteArray";
            var value = new byte[] { 114, 76 };

            Client.Set(key, value);
            var fetchedValue = Client.Get<byte[]>(key);

            Assert.IsNotNull(fetchedValue);
            Assert.AreEqual(value[0], fetchedValue[0]);
            Assert.AreEqual(value[1], fetchedValue[1]);
        }

        [TestMethod]
        public void TestSetFloat()
        {
            string key = "TestGetSet.TestSetFloat";
            float value = -92374.2345345f;

            Client.Set(key, value);
            float fetchedValue = Client.Get<float>(key);

            Assert.AreEqual(value, fetchedValue);
        }

        [TestMethod]
        public void TestSetDouble()
        {
            string key = "TestGetSet.TestSetDouble";
            double value = -234534523.23454353;

            Client.Set(key, value);
            double fetchedValue = Client.Get<double>(key);

            Assert.AreEqual(value, fetchedValue);
        }

        [TestMethod]
        public void TestSetChar()
        {
            string key = "TestGetSet.TestSetChar";
            char value = 'A';

            Client.Set(key, value);
            char fetchedValue = Client.Get<char>(key);

            Assert.AreEqual(value, fetchedValue);
        }

        [TestMethod]
        public void TestSetBool()
        {
            string key = "TestGetSet.TestSetBool";
            bool value = true;

            Client.Set(key, value);
            bool fetchedValue = Client.Get<bool>(key);

            Assert.AreEqual(value, fetchedValue);
        }

        [TestMethod]
        public void TestSetString()
        {
            string key = "TestGetSet.TestSetString";
            string value = "Test value 123";

            Client.Set(key, value);
            string fetchedValue = Client.Get<string>(key);

            Assert.AreEqual(value, fetchedValue);
        }

        [TestMethod]
        public void TestSetDecimal()
        {
            string key = "TestGetSet.TestSetDecimal";
            decimal value = 43543.234234M;

            Client.Set(key, value);
            decimal fetchedValue = Client.Get<decimal>(key);

            Assert.AreEqual(value, fetchedValue);
        }

        [TestMethod]
        public void TestSetDateTime()
        {
            string key = "TestGetSet.TestSetDateTime";
            DateTime value = new DateTime(2015, 12, 27, 12, 00, 00);

            Client.Set(key, value);
            DateTime fetchedValue = Client.Get<DateTime>(key);

            Assert.AreEqual(value, fetchedValue);
        }

        [TestMethod]
        public void TestSetObject()
        {
            string key = "TestGetSet.TestSetObject";
            ServerDefinition value = new ServerDefinition("localhost", 432123, 5000);

            Client.Set(key, value);
            ServerDefinition fetchedValue = Client.Get<ServerDefinition>(key);

            Assert.IsNotNull(fetchedValue);
            Assert.AreEqual(value.Address, fetchedValue.Address);
            Assert.AreEqual(value.Port, fetchedValue.Port);
            Assert.AreEqual(value.SendReceiveTimeout, fetchedValue.SendReceiveTimeout);
        }

        [TestMethod]
        public void TestStoringValueWithExpirationTime()
        {
            string key = "TestGetSet.TestStoringValueWithExpirationTime";
            DateTime value = new DateTime(2015, 12, 27, 12, 00, 00);

            Client.Set(key, value, 3600);
            DateTime fetchedValue = Client.Get<DateTime>(key);

            Assert.AreEqual(value, fetchedValue);
        }
    }
}
